package com.healthcloud.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.healthcloud.annotation.SysLog;
import com.healthcloud.entity.MsgVo;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.PageParam;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.healthcloud.entity.BaseDepartmentEntity;
import com.healthcloud.service.BaseDepartmentService;
import springfox.documentation.annotations.ApiIgnore;


/**
 * 科室标准信息
 * 
 * @author yucl
 * @email yucailong1111@126.com
 * @date 2017-08-29 16:54:41
 */
@ApiIgnore
@Controller
@RequestMapping("basedepartment")
public class BaseDepartmentController {
	@Autowired
	private BaseDepartmentService baseDepartmentService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("basedepartment:list")
	public String list(@RequestParam Map<String, Object> params){
		return "bus/basedepartment/basedepartmentmgr";
	}
	
	@RequiresPermissions("basedepartment:list")
	@RequestMapping(value = "/searchBaseDepartment")
	@ResponseBody
    public MsgVo  searchBaseDepartment(BaseDepartmentEntity bean,Model model, @ModelAttribute("pageParam")PageParam pageParam){
        List<BaseDepartmentEntity> list = baseDepartmentService.queryList(bean, pageParam);
        int count = baseDepartmentService.queryTotal(bean);
        return new MsgVo(list,count,count,pageParam.getsEcho());
    }
	
	/**
	 * 新增页
	 */
	@RequestMapping("/tosave")
	@RequiresPermissions("basedepartment:save")
    public String toSave(Model model) {
        return "bus/basedepartment/addOrUpdatebasedepartment";
    }
    
	/**
	 * 保存
	 */
    @SysLog("保存BaseDepartment")
    @RequestMapping("/save")
    @ResponseBody
    public ApiRes save(BaseDepartmentEntity entity) {
        baseDepartmentService.save(entity);
        return ApiRes.SUCCESS;
    }
	
	/**
	 * 修改页
	 */	
	@RequestMapping("/toupdate")
    @RequiresPermissions("basedepartment:update")
    public String toUpdate(@Param("id") Integer id, Model model) {

        BaseDepartmentEntity entity = baseDepartmentService.queryObject(id);
        model.addAttribute("entity", entity);

        return "bus/basedepartment/addOrUpdatebasedepartment";
    }
	
    /**
     * 修改
     */
    @SysLog("修改BaseDepartment")
    @RequestMapping("/update")
    @ResponseBody
    public ApiRes update(BaseDepartmentEntity entity) {
        baseDepartmentService.update(entity);
        return ApiRes.SUCCESS;
    }

	
    /**
     * 删除
     */
    @SysLog("删除BaseDepartment")
    @RequestMapping("/delete")
    @RequiresPermissions("basedepartment:delete")
    @ResponseBody
    public ApiRes delete(Long[] ids) {
        baseDepartmentService.logicalDeletes(ids);
        return ApiRes.SUCCESS;
    }


    /**
     * ajax验证科室是否存在
     *
     *  */
    @RequestMapping(value = "/validateDepName")
    public @ResponseBody Object validateDepName(String depName, String exception){
        Map<String,Boolean> map = new HashMap<>();
        if(depName!=null && exception!=null && depName.trim().equals(exception.trim())){
            map.put("valid",true);
            return map;
        }
        if(depName==null||"".equals(depName.trim())){
            map.put("valid",false);
            return map;
        }
        BaseDepartmentEntity entity = baseDepartmentService.queryByDepName(depName);
        if (entity==null) {
            map.put("valid",true);
            return map;
        } else {
            map.put("valid",false);
            return map;
        }
    }

}
